package edu.nf.api.controller.shop;

import edu.nf.api.controller.BaseController;
import edu.nf.api.fileutil.FileUploadUtil;
import edu.nf.waimai.dao.shop.ShopDAO;
import edu.nf.waimai.dao.shop.impl.ShopDAOImpl;
import edu.nf.waimai.model.shop.Shop;
import edu.nf.waimai.model.shop.ShopType;
import edu.nf.waimai.service.shop.ShopService;
import edu.nf.waimai.service.shop.impl.ShopServiceImpl;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.util.List;

public class ShopController extends BaseController {
    private ShopService service = new ShopServiceImpl();

    /**
     * 查询全部店铺
     *
     * @return
     */
    @RequestMapping("/shop/getListShop")
    public View listShop() {
        JsonView jsonView = new JsonView(success(service.listShop()));
        jsonView.setDateFormat("yyyy-MM-dd");
        return jsonView;
    }

    /**
     * 通过名称查询店铺
     *
     * @param shopName
     * @return
     */
    @RequestMapping("/shop/getListShopByName")
    public View getShopByShopName(@RequestParam("shopName") String shopName) {
        System.out.println("shopName = " + shopName);
        return new JsonView(service.listShopByShopName(shopName));
    }

    /**
     * 店铺分页
     *
     * @param shop
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/shop/getListShopPaging")
    public View listShopPaging(@Model Shop shop, @RequestParam("page") int page, @RequestParam("limit") int limit) {
        return new JsonView(success(service.listShopPaging(shop, page, limit)));
    }

    /**
     * 店铺分类分页
     *
     * @param shop
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/shop/getListTypeShopPaging")
    public View listTypeShopPaging(@Model Shop shop, @RequestParam("page") int page, @RequestParam("limit") int limit) {
        ShopDAO dao = new ShopDAOImpl();
        List<Shop> list = dao.listTypeShopPaging(shop, page, limit);
        return new JsonView(success(list));
    }

    @RequestMapping("/shop/getShopType")
    public View getShopType(@RequestParam("id") int id) {
        ShopDAO dao = new ShopDAOImpl();
        ShopType shopType = dao.getShopType(id);

        return new JsonView(success(shopType));
    }

    @RequestMapping("/shop/uploadImg")
    public View userUploadImg(Part[] file, HttpServletRequest request, HttpServletResponse response) {
        String filename = "";
        for (Part part1 : file) {
            //获取路径
            String filePath = request.getServletContext().getRealPath("/images/shop");
            //创建工具类
            FileUploadUtil fileUploadUtil = new FileUploadUtil();
            //设置文件大小
            fileUploadUtil.setMaxSize(1 * 1024 * 1024);
            //设置文件的后缀
            fileUploadUtil.setAllowFileExt("jpg,png,jpeg");
            //文件名称
            filename = fileUploadUtil.upload(part1, filePath);

        }
        return new JsonView(success(filename));
    }

    @RequestMapping("/shop/getShop")
    public View getShopById(@RequestParam("uid") int uid) {
        ShopDAO dao = new ShopDAOImpl();
        return new JsonView(success(dao.getShop(uid)));
    }

//    private final String apiKey = "839bfb965ce089afc2306a5e7ee29000"; // 你的高德地图 API 密钥
    //解决高德地图的跨域
//    @RequestMapping("/api/geocode/regeo")
//    public View proxyGeocodeRegeo(@RequestParam("location") String location,@RequestParam("latitude")String latitude){
//        String s = "https://restapi.amap.com/v3/geocode/regeo?output=JSON&location="+location+","+latitude+"&key=839bfb965ce089afc2306a5e7ee29000&radius=1000&extensions=all";
//        return new JsonView(success());
//    }
}
